	
	********************************************************************
	** WAVE 4 POST-PLANTING DATA
	********************************************************************
	
	** WAVE 4 POST PLANTING
	** Roster, labor, and time use
	
	use "$dir/rawdata/LSMS Nigeria/2018/sect1_plantingw4.dta", clear // ROSTER
	merge 1:1 hhid indiv using "$dir/rawdata/LSMS Nigeria/2018/sect3_plantingw4.dta", gen(merge_sec3) assert(master matched) // LABOR
	merge 1:1 hhid indiv using "$dir/rawdata/LSMS Nigeria/2018/sect3b_plantingw4.dta", gen(merge_sec3b) assert(master matched) // TIME USE
	
	***********
	***********

	** INFORMATION FROM HOUSEHOLD ROSTER

		** Still in HH
		gen w4p_inhh=s1q4!=2

		** Household size
		gen temp=1 if w4p_inhh==1
		bys hhid: egen w4p_hh_size=total(temp)
		drop temp

		** Number of children 12 or younger
		gen temp=1 if s1q6<=12 & w4p_inhh==1
		bys hhid: egen w4p_hh_under12=total(temp)
		drop temp

		** Number of youth 18 or younger
		gen temp=1 if s1q6<=18 & w4p_inhh==1
		bys hhid: egen w4p_hh_under18=total(temp)
		drop temp

		** Number of adults 60 or older
		gen temp=1 if s1q6>=60 & !missing(s1q6) & s1q6!=999 & w4p_inhh==1
		bys hhid: egen w4p_hh_over60=total(temp)
		drop temp

		** Adults 12 to 60
		gen temp=1 if s1q6>12 & s1q6<60 & !missing(s1q6) & s1q6!=999 & w4p_inhh==1
		bys hhid: egen w4p_hh_12to60=total(temp)
		drop temp

		** Marital status
		gen w4p_married=(s1q8==1 | s1q8==2) if !missing(s1q8)
		gen w4p_married_monog=(s1q8==1) if !missing(s1q8)
		gen w4p_married_polyg=(s1q8==2) if !missing(s1q8)

		** Female
		gen w4p_female=(s1q2==2) if !missing(s1q2)

		** Age
		gen w4p_age=s1q6
		replace w4p_age=. if w4p_age==999

		** Age of spouse
		sum indiv 
		forvalues x=1/`r(max)' {
			gen age_person`x'_temp=s1q6 if indiv==`x' & !missing(s1q6)
			bys hhid: egen age_person`x'=max(age_person`x'_temp)
			drop age_person`x'_temp
		}

		** Average age of others in the household
		sum indiv 
		forvalues x=1/`r(max)' {
			replace age_person`x'=. if indiv==`x'
		}
		egen w4p_hh_avgage=rowmean(age_person*)

		** Spouse age
		sum indiv
		forvalues x=1/`r(max)' {
			replace age_person`x'=. if s1q13_1!=`x' // FIXME: Could do this more carefully since several indices for spouses allowed
		}
		egen w4p_spouse_age=rowmean(age_person*)

		** Age gap
		gen w4p_spouse_agegap=w4p_age-w4p_spouse_age

		** Birthday
		gen w4p_birthday=mdy(6,1,s1q7_year) // FIXME: Don't have birth month or date
		format w4p_birthday %td

		** Birth year
		gen w4p_birthyear=s1q7_year
		recode w4p_birthyear 9999=. 9998=.
		replace w4p_birthyear=2018-w4p_age if missing(w4p_birthyear)
		
	***********
	***********
	
	** Geographic information
	
		gen w4p_geo_urban=(sector==1) if !missing(sector)
		gen w4p_geo_state=state
		gen w4p_geo_lga=lga
		replace w4p_geo_lga=s1q32b if !missing(s1q32b) // LGA code for where the HH member moved
		label var w4p_geo_urban "Urban residence"
		label var w4p_geo_state "State of residence"
		label var w4p_geo_lga "LGA of residence" 
		
	***********
	***********

	** LABOR

		** Worked for a non-HH member
		gen w4p_work_outsideHH=s3q4==1 if !missing(s3q4)
		gen w4p_work_outsideHH_hrs=s3q4b
		replace w4p_work_outsideHH_hrs=0 if w4p_work_outsideHH==0

		** Worked in agriculture for a HH member
		gen w4p_work_agHH=s3q5==1 if !missing(s3q5)
		gen w4p_work_agHH_hrs=s3q5b
		replace w4p_work_agHH_hrs=0 if w4p_work_agHH==0

		** Worked in HH business
		gen w4p_work_bizHH=s3q6==1 if !missing(s3q6)
		gen w4p_work_bizHH_hrs=s3q6b
		replace w4p_work_bizHH_hrs=0 if w4p_work_bizHH==0

		** Any work in last 7 days (agriculture, for non-HH member, own account/business)
		gen w4p_worked_last7days=0
		replace w4p_worked_last7days=1 if s3q4==1
		replace w4p_worked_last7days=1 if s3q5==1 
		replace w4p_worked_last7days=1 if s3q6==1
		replace w4p_worked_last7days=. if missing(s3q4) & missing(s3q5) & missing(s3q6)

		** Hours worked in last 7 days
		egen w4p_work_hrs=rowtotal(w4p_work_outsideHH_hrs w4p_work_agHH_hrs w4p_work_bizHH_hrs)
		replace w4p_work_hrs=. if missing(w4p_work_outsideHH_hrs) & missing(w4p_work_agHH_hrs) & missing(w4p_work_bizHH_hrs)

		** Works in agriculture
		gen w4p_work_agri=(w4p_work_agHH==1 | s3q14==1) if (!missing(w4p_work_agHH) | !missing(s3q14))
	
	***********
	***********
	
	** TIME USE
		
		** Spent time providing care for adult in HH
		gen w4p_careadults_any=s3bq4__10==1 if !missing(s3bq4__10)
		
		** Hours spent providing care for adult in HH
		gen w4p_careadults_hours=s3bq14
		
	**********
	***********
	
	** HH CONSUMPTION
	
		merge m:1 hhid using "$dir/rawdata/LSMS Nigeria/2018/totcons_final.dta", assert(master matched) nogen
		gen w4p_urban_fromcons=sector==1 if !missing(sector)
		ren totcons_pc w4p_conspc
		ren hhsize w4p_hhsize_fromcons

	**********
	**********
	
		keep hhid indiv w4p*
		order hhid indiv w4p*
		save "$dir/data/NGA_w4p_individual.dta", replace
